#    This file is part of robo-ircbot.
#
#    robo-ircbot is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    robo-ircbot is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with robo-ircbot.  If not, see <http://www.gnu.org/licenses/>.

package App::Modular::Module::Wikipedia;
use strict;
use warnings;
use LWP::UserAgent;
use base qw(App::Modular::Module);

print ":: Wikipedia module loaded.\n";

# Events we want to receive.
sub events { return "PRIVMSG"; }

sub help {
        return (
                "Search for Wikipedia articles and return back a link.",
                "wiki <search term> [/max=<num>]"
        );
}

# PRIVMSG events.
sub event_privmsg
{
        shift;
        my $e = shift;

        my $help = $e->robo->mod("Helper") || return;

        # Admin commands for wiki module.
        if ($help->trigger_switch("wiki", $e)) {
                return;
        }

        # Wiki trigger.
        if (my $h = $help->trig("wiki", $e, 1)) {
                my $max = $h->{max} || 1;
                read_wiki($e, $h->{data}, $max);
        }
}

# Read the wikipedia HTML.
sub read_wiki
{
        my ($e, $search, $max) = @_;

        # Download the HTML content.
        # The get() function downloads any text file from the web
        # and will store it in a variable.
        my $ua = LWP::UserAgent->new(agent => "Mozilla/5.0 (X11; U; Linux i686; ".
                                              "en-US; rv:1.9.0.2) ".
                                              "Gecko/2008092313 Ubuntu/8.04 ".
                                              "(hardy) Firefox/3.1");

        my $response = $ua->get("http://en.wikipedia.org/wiki/Special:Search?search=".
                                $search."&fulltext=Search");
        if (!$response->is_success) {
                $e->s("PRIVMSG ".$e->dest." :\002wiki\002: Could not perform search!");
                return;
        }

        my $content = $response->content;

        # Show top 3 or $max search results.
        my ($title, $url);
        $max = 3 unless $max;
        my $i = 0;
        while ($content =~ m/<li><a href="\/wiki\/([^"]+)" title="([^"]+)">/g) {
                last if ($i++ == $max);
                $url = $1;
                $title = $2; $title =~ s/\&\#039;/\'/g;
                $e->s("PRIVMSG ".$e->dest." :\002wiki\002: ".$title.
                      " - http://en.wikipedia.org/wiki/".$url);
                sleep(1);
        }

        # No results came.
        $e->s("PRIVMSG ".$e->dest." :\002wiki\002: No results returned!") if ($i == 0);
}

1;
